<template>
   <div class="pages">
     <div class="body">
       <div class="page__bd page__bd_spacing">

               <div class="icon-box">
                   <i class="weui-icon-info weui-icon_msg"></i>

               </div>

           </div>
       <span style="width: 100%;color: #0BB20C;font-size: 36rpx;margin:20rpx 0">接受授权</span>
       <span style="width: 100%;margin-bottom: 20rpx;font-size: 22rpx">为了更好的为您提供服务,此应用需要您的授权</span>
      <button
           open-type="getUserInfo"
            type="defult"
            form-type="submit"
            size="mini"
            @getuserinfo="wxLogin"

             style="width: 80%;background: #0BB20C;color: #fff;">授权登录</button>
     </div>

   </div>
</template>

<script>
  export default{
    data () {
      return {
        code: ''
      }
    },
    onShow () {
      wx.setNavigationBarTitle({
        title: '授权登录'
      })
      let token = wx.getStorageSync('Authorization')
      if (token) {
        wx.navigateTo({ url: '/pages/index/main' })
      }
      // wx.navigateTo({ url: '/pages/index/main' })
    },
    methods: {
      wxLogin (e) {
        let that = this
       // console.log(e)
        wx.login({
          success (res) {
         //   console.log(res)
            that.code = res.code
            if (e.mp.detail.userInfo) {
              that.$http.request({
                method: 'post',
                url: '/api/wxuser/login',
                body: {
                  code: res.code,
                  encryptedData: e.mp.detail.encryptedData,
                  iv: e.mp.detail.iv,
                  rawData: e.mp.detail.rawData,
                  signature: e.mp.detail.signature
                }
              }).then((data) => {
               // console.log(data)
                if (data.code == 200) {
                  wx.setStorageSync('Authorization', data.data)
                  wx.showToast({
                    title: '登录成功!',
                    icon: 'success',
                    duration: 2000,
                    success: () => {
                      wx.setStorageSync('userInfo', JSON.stringify(e.mp.detail.rawData))
                      wx.navigateTo({ url: '/pages/index/main' })
                    }
                  })
                } else {
                  wx.showToast({
                    title: '登录失败,请重新登陆!',
                    icon: 'none',
                    duration: 2000

                  })
                }
              }).catch((err) => {
                wx.showToast({
                  title: `${err}`,
                  icon: 'none',
                  duration: 2000

                })
              })
            } else {

            }
            //    wx.setStorageSync('openid',  that.code)
            // that.setAppletCode(res.code)
            // that.wxGetUserInfo(res.code)
          }
        })
      },

      wxGetUserInfo (code) {
        const that = this
        wx.getUserInfo({
          withCredentials: true,
          success (res) {
         //   console.log(res)
          },
          fail (err) {
         //   console.log('自动wx.getUserInfo失败:', err)
            // 显示主动授权的button
            that.buttonVisible = true
          }
        })
      }
    }
  }
</script>

<style scoped>
  .body{
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding-top:100rpx;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
  }
</style>
